import pinyin from 'pinyin'
export default class Song {
  constructor({ id, name, singer, image, album, aliaName }) {
    this.id = id
    this.name = name
    this.singer = singer
    this.image = image
    this.album = album
    this.aliaName = aliaName
  }
}

function singerName(artists) {
  const name = artists.map(artist => {
    return artist.name
  })
  return name.join('/')
}

export function dealWithSong(item) {
  return new Song({
    id: item.id,
    name: item.name,
    singer: singerName(item.song.artists),
    image: item.song.album.picUrl,
    album: item.song.album
  })
}

export function buildTopArtists(list) {
  const artists = {
    tag: '热门',
    list: []
  }
  list.forEach((item, index, array) => {
    if (index < 10) {
      artists.list.push(item)
    }
  })
  return artists
}
export function buildArtistsList(list) {
  const artists = {}
  list.map(item => {
    let py = pinyin(item.name[0], {
      style: pinyin.STYLE_FIRST_LETTER
    })
    item.initials = py[0][0].toUpperCase()
  })
  list.sort((a, b) => {
    return a.initials.localeCompare(b.initials, 'en')
  })
  list.forEach((item, index, array) => {
    let key = item.initials
    if(key.match(/[A-Z]/)){
      if ( !artists[key] ){
        artists[key] = {
          tag: key,
          list: []
        }
      }
      artists[key].list.push(item)
    }
  })

  // const orderList = []
  // const topList = []
  // for(const key in artists){
  //   key == 'hot'
  //     ? topList.push(artists[key])
  //     : orderList.push(artists[key])
  // }
  // orderList.sort((a, b) => {
  //   return a.tag.localeCompare(b.tag, 'en')
  // })
  // console.log(orderList)
  return artists
}